<?php
namespace app\common\model;

use app\common\model\Base;

class Notice extends Base
{
    protected $insert=['create_time'];

    protected function setCreateTimeAttr(){
        return time();
    }

    /**
     * 获取分页列表
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public function getPageDatas($where,$page,$perpagenum){
        $start = ($page-1)*$perpagenum;

        $whereData=[];
        if(!empty($where)){
            foreach($where as $k => $wh):
                $whereData['a.'.$k] = $wh;
            endforeach;
        }
        
        $rows = $this
                ->alias('a')
                ->join('admin b','a.admin_id=b.id')
                ->where($whereData)
                ->field('a.*,b.username username')
                ->limit($start,$perpagenum)
                ->order('a.id','DESC')->select();

        $count = $this->alias('a')->where($whereData)->count();
        $data = [];
        if($count){
            foreach($rows as $row):
                $row = $row->toArray();
                $data[] = $this->setFields($row);
            endforeach;
        }

        $total_page = ceil($count/$perpagenum);
        return [
            'total_page' => $total_page,
            'curent_page' => $page,
            'count' => $count,
            'data' => $data
        ];
    }

    public function getOneData($id){
        $data = $this
                ->alias('n')
                ->join('admin a','n.admin_id=a.id')
                ->field('n.*,a.username')
                ->where('n.id',$id)
                ->find();

        if($data){
            $data = $data->toArray();
            $data = $this->setFields($data);
        }
        return $data;
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public function setFields($row){
        if(isset($row['create_time'])){
			$row['create_date'] = date("Y-m-d H:i",$row['create_time']);
		}
        return $row;
    }

}